/**
 ******************************************************************************
 * @file    netconf.c
 * @author  MCD Application Team
 * @version V1.1.0
 * @date    31-July-2013
 * @brief   Network connection configuration
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
 *
 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *        http://www.st.com/software_license_agreement_liberty_v2
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************
 */

/* Includes ------------------------------------------------------------------*/
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/dhcp.h"
#include "stm32f4xx_eth_bsp.h"
#include "lwip/lwip-1.4.1/port/STM32F4x7/FreeRTOS/ethernetif.h"
#include "main.h"
#include "parameters.h"
#include "netconf.h"
#include "tcpip.h"
#include <stdio.h>

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define MAX_DHCP_TRIES 4

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
struct netif xnetif; /* network interface structure */
__IO uint8_t DHCP_state;

/* Private functions ---------------------------------------------------------*/
/**
 * @brief  Initializes the lwIP stack
 * @param  None
 * @retval None
 */

void LwIP_Init(void)
{
	struct ip_addr ipaddr;
	struct ip_addr netmask;
	struct ip_addr gw;

	/* Create tcp_ip stack thread */
	tcpip_init( NULL, NULL );

	/* IP address setting */
	if(startParam.net.net_use & E_NETWORK_USE_DHCP)
	{
		ipaddr.addr = 0;
		netmask.addr = 0;
		gw.addr = 0;
	}
	else
	{
		IP4_ADDR(&ipaddr, startParam.net.ip_addr[0], startParam.net.ip_addr[1],
				startParam.net.ip_addr[2], startParam.net.ip_addr[3]);
		IP4_ADDR(&netmask, startParam.net.sub[0], startParam.net.sub[1],
				startParam.net.sub[2], startParam.net.sub[3]);
		IP4_ADDR(&gw, startParam.net.gw_addr[0], startParam.net.gw_addr[1],
				startParam.net.gw_addr[2], startParam.net.gw_addr[3]);
	}

	netif_add(&xnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
	netif_set_default(&xnetif);

	if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
	{
		/* Set Ethernet link flag */
		xnetif.flags |= NETIF_FLAG_LINK_UP;

		/* When the netif is fully configured this function must be called.*/
		netif_set_up(&xnetif);

		//if(netStruct.DHCP_use == 1)
		DHCP_state = DHCP_START;
	}
	else
	{
		/*  When the netif link is down this function must be called.*/
		netif_set_down(&xnetif);
		if(startParam.net.net_use & E_NETWORK_USE_DHCP)
			DHCP_state = DHCP_LINK_DOWN;
	}

	/* Set the link callback function, this function is called on change of link status*/
	netif_set_link_callback(&xnetif, ETH_link_callback);
}

/**
 * @brief  LwIP_DHCP_Process_Handle
 * @param  None
 * @retval None
 */
void LwIP_DHCP_task(void * pvParameters)
{
	uint32_t IPaddress;
	for (;;)
	{
		switch (DHCP_state)
		{
		case DHCP_START:
		{
			dhcp_start(&xnetif);
			/* IP address should be set to 0
           every time we want to assign a new DHCP address */
			IPaddress = 0;
			DHCP_state = DHCP_WAIT_ADDRESS;
		}
		break;

		case DHCP_WAIT_ADDRESS:
		{
			/* Read the new IP address */
			IPaddress = xnetif.ip_addr.addr;

			if (IPaddress!=0)
			{
				DHCP_state = DHCP_ADDRESS_ASSIGNED;

				/* Stop DHCP */
				dhcp_stop(&xnetif);
			}
			else
			{
				/* DHCP timeout */
				if (xnetif.dhcp->tries > MAX_DHCP_TRIES)
				{
					DHCP_state = DHCP_TIMEOUT;
					/* Stop DHCP */
					dhcp_stop(&xnetif);
				}
			}
		}
		break;
		default: break;
		}
		vTaskDelay(150);
	}
}
